package com.example.chiro;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Slide_general extends Fragment {

	public static Intent locatorService = null;
	public static AlertDialog alertDialog = null;
	public static ViewGroup root = null;
	public static ImageButton rafraichir;
	public static ProgressBar rafraichir_chargement;
	public static RelativeLayout rafraichir_chargement_rl;
	
	public static TextView longitude, latitude, altitude, nom, prenom, date,
			numerofiche, maj;
	public static String numerofiche_value = "", // cette valeur servira lors de
													// l'envoie sur la base
													// sqlite à renseigner le
													// champs 'id_fiche' des
													// tables cavité et espèce
			date_value = new SimpleDateFormat("dd/MM/yyyy", Locale.FRANCE)
					.format(new Date()),
			nom_value = "lopez",
			prenom_value = "matthieu";

	
	GetGPS tache_getGPS;
	
	public static Fragment newInstance(Context context) {
		Log.i("t: Slide_general", "newInstance : debut");
		Slide_general f = new Slide_general();
		Log.i("t: Slide_general", "newInstance : fin");
		return f;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.i("t: Slide_general", "oCView : debut");
		root = (ViewGroup) inflater.inflate(R.layout.slide_general,
				null);
		// gpstracker = new GPSTracker(root.getContext());

		longitude = (TextView) root.findViewById(R.id.longitude);
		latitude = (TextView) root.findViewById(R.id.latitude);
		altitude = (TextView) root.findViewById(R.id.altitude);
		date = (TextView) root.findViewById(R.id.date);
		nom = (TextView) root.findViewById(R.id.nom);
		prenom = (TextView) root.findViewById(R.id.prenom);
		numerofiche = (TextView) root.findViewById(R.id.numero_fiche);
		maj = (TextView) root.findViewById(R.id.maj);

		date.setText(new SimpleDateFormat("dd/MM/yyyy", Locale.FRANCE)
				.format(new Date()));
		nom.setText(nom_value);
		prenom.setText(prenom_value);
		numerofiche.setText(numerofiche_value);

		rafraichir = (ImageButton) root.findViewById(R.id.rafraichir);
		rafraichir_chargement = (ProgressBar) root.findViewById(R.id.rafraichir_chargement);
		rafraichir_chargement_rl = (RelativeLayout) root.findViewById(R.id.rafraichir_chargement_rl);
		
		
		rafraichir.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				final LocationManager manager = (LocationManager)root.getContext().getSystemService(root.getContext().LOCATION_SERVICE);
				if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
					showSettingsAlert();
					return;
				}
				tache_getGPS = new GetGPS();
				tache_getGPS.execute();
				rafraichir.setVisibility(View.GONE);
				rafraichir_chargement.setVisibility(View.VISIBLE);
				rafraichir_chargement_rl.setVisibility(View.VISIBLE);
			}
		});
		
		
		rafraichir_chargement.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				tache_getGPS.cancel(true);
				rafraichir.setVisibility(View.VISIBLE);
				rafraichir_chargement.setVisibility(View.GONE);
				rafraichir_chargement_rl.setVisibility(View.GONE);
			}
		});
		
		
		// rafraichir();

		Log.i("t: Slide_general", "oCView : fin");
		return root;
	}

	public boolean stopService() {
		if (this.locatorService != null) {
			this.locatorService = null;
		}
		return true;
	}

	public void onStop() {
		Log.i("t: Slide_general", "oS : debut");
		super.onStop();
		// gpstracker.stopUsingGPS();
		Log.i("t: Slide_general", "oS : fin");
	}

	/*private void rafraichir() {
		Log.i("t: Slide_general", "rafraichir : debut");
		gpstracker = new GPSTracker(this.getActivity());

		if (gpstracker.canGetLocation()) {
			longitude_value = String.valueOf(gpstracker.getLongitude());
			latitude_value = String.valueOf(gpstracker.getLatitude());
			altitude_value = String.valueOf(gpstracker.getAltitude());
			longitude.setText(longitude_value);
			latitude.setText(latitude_value);
			altitude.setText(altitude_value);

			// afficher_adresse();

			
			 * Arbre.donnees_general.setLatitude(Double.parseDouble(latitude
			 * .getText().toString()));
			 * Arbre.donnees_general.setLongitude(Double.parseDouble(longitude
			 * .getText().toString()));
			 * Arbre.donnees_general.setAltitude(Double.parseDouble(altitude
			 * .getText().toString())); if (adresse() != null) {
			 * Arbre.donnees_general.setCommune(adresse().getLocality());
			 * Arbre.donnees_general.setLieu_dit(adresse() .getThoroughfare());
			 * }
			 
		} else {
			// gpstracker.showSettingsAlert();
			if (gpstracker.canGetLocation()) {
				longitude_value = String.valueOf(gpstracker.getLongitude());
				latitude_value = String.valueOf(gpstracker.getLatitude());
				altitude_value = String.valueOf(gpstracker.getAltitude());
				longitude.setText(longitude_value);
				latitude.setText(latitude_value);
				altitude.setText(altitude_value);

				// afficher_adresse();

				
				 * Arbre.donnees_general.setLatitude(Double.parseDouble(latitude
				 * .getText().toString()));
				 * Arbre.donnees_general.setLongitude(Double
				 * .parseDouble(longitude .getText().toString()));
				 * Arbre.donnees_general.setAltitude(Double.parseDouble(altitude
				 * .getText().toString())); if (adresse() != null) {
				 * Arbre.donnees_general.setCommune(adresse().getLocality());
				 * Arbre.donnees_general.setLieu_dit(adresse()
				 * .getThoroughfare()); }
				 
			}
		}
		gpstracker.stopUsingGPS();
		Log.i("t: Slide_general", "rafraichir : fin");
	}*/

	

	public static ContentValues recolte_donnees() {

		ContentValues initialValues = new ContentValues();

		initialValues.put(Table_general.DATE, date_value);
		initialValues.put(Table_general.NOM, String_operation.op_string(nom_value));
		initialValues.put(Table_general.PRENOM, String_operation.op_string(prenom_value));
		initialValues.put(Table_general.LONGITUDE, String_operation.op_string((String) longitude.getText()));
		initialValues.put(Table_general.LATITUDE, String_operation.op_string((String) latitude.getText()));
		initialValues.put(Table_general.ALTITUDE, String_operation.op_string((String) altitude.getText()));

		return initialValues;
	}
	
	private void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(root.getContext());

        // Setting Dialog Title
        alertDialog.setTitle("Configuration GPS");

        // Setting Dialog Message
        alertDialog
                .setMessage("Le GPS n'est pas activé. Voulez vous aller aux options de votre appareil?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Options",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(
                                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        root.getContext().startActivity(intent);
                    }
                });

        // on pressing cancel button
        alertDialog.setNegativeButton("Annuler",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        // Showing Alert Message
        alertDialog.show();
    }

}
